<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\MemberQuality as MemberQualityModel;
use app\common\model\Member as MemberModel;
use app\common\model\Config as ConfigModel;
use app\common\model\Wechat as WechatModel;
use app\common\model\Qq as QqModel;

/**
 * Memberquality 用户修为记录
 * Class Memberquality
 * @package app\home\controller
 */
class Memberquality extends Base
{
    /**
     * [getquality  获取修为]
     * @return json
     * @author [C。]
     */
    public function getquality()
    {
        $param = input('param.');
        $model = new MemberQualityModel;
        $cmodel = new ConfigModel;
        $wmodel = new WechatModel;
        $qmodel = new QqModel;
        $user = $this->user;
        $data = [];
        try{
            $http = substr($user['img'],0,4);
            if($http != 'http') {
                $data['header_img'] = config('domain_name') . $user['img'];
            }else{
                $data['header_img'] = $user['img'];
            }
            $data['username'] = $user['username'];
            $data['quality'] = $user['quality'];

            $today_quality = $model->getSum(['mid'=>$user['id'],'create_time'=>['between',[strtotime(date('Y-m-d')),strtotime(date('Y-m-d')) + 24*3600]]],'quality');
            $data['today_quality'] = $today_quality;
            $is_continuous = $cmodel->findData('is_continuous','cname','cvalue');
            if(empty($is_continuous['cvalue'])){
                $data['is_continuous'] = config('quality.is_continuous');
            }else{
                $data['is_continuous'] = $is_continuous['cvalue'];
            }

            $quality = $model->findOne(['mid'=>$user['id'],'type'=>40],'create_time,day','id','desc');
            if(strtotime($quality['create_time']) >= strtotime(date('Y-m-d'))){
                $data['day'] = $quality['day'];
                $data['is_qian'] = 1;
            }elseif(strtotime($quality['create_time']) < strtotime(date('Y-m-d')) && strtotime($quality['create_time']) >= strtotime(date('Y-m-d'))-24*3600){
                if($quality['day'] >= 7){
                    $data['day'] = 0;
                }else{
                    $data['day'] = $quality['day'];
                }
                $data['is_qian'] = 2;
            }else{
                if($data['is_continuous'] == 1){
                    $data['day'] = 0;
                }else{
                    if($quality['day'] >= 7 || empty($quality['day'])){
                        $data['day'] = 0;
                    }else{
                        $data['day'] = $quality['day'];
                    }
                }
                $data['is_qian'] = 2;
            }
            $daily_signin = $cmodel->findData('daily_signin','cname','cvalue');
            if(empty($daily_signin['cvalue'])){
                $daily_signin = config('quality.daily_signin');
            }else{
                $daily_signin = unserialize($daily_signin['cvalue']);
            }
            $data['daily_signin'] = $daily_signin;

            $quality_leavel = $cmodel->findData('quality_leavel','cname','cvalue');
            if(empty($quality_leavel['cvalue'])){
                $quality_leavel = config('quality.quality_leavel');
            }else{
                $quality_leavel = unserialize($quality_leavel['cvalue']);
            }
            $data['quality_level'] = $quality_leavel;
            foreach($quality_leavel as $k=>$v){
                if($user['quality'] >= $v['quality'] && $user['quality'] < $quality_leavel[$k+1]['quality']){
                    $data['level'] = $v['name'];
                    $data['level_num'] = $v['level'];
                }
            }

            $help_people = $cmodel->findData('help_people','cname','cvalue');
            if(empty($help_people['cvalue'])){
                $help_people = config('quality.help_people');
            }else{
                $help_people = unserialize($help_people['cvalue']);
            }
            foreach($help_people as &$v){
                $hasnum = $model->getAllCount(['mid'=>$user['id'],'type'=>$v['type'],'create_time'=>['between',[strtotime(date('Y-m-d')),strtotime(date('Y-m-d')) + 24*3600]]]);
                $v['hasnum'] = $hasnum;
            }
            $data['help_people'] = $help_people;

            $work_hard = $cmodel->findData('work_hard','cname','cvalue');
            if(empty($work_hard['cvalue'])){
                $work_hard = config('quality.work_hard');
            }else{
                $work_hard = unserialize($work_hard['cvalue']);
            }
            foreach($work_hard as &$v){
                $hasnum = $model->getAllCount(['mid'=>$user['id'],'type'=>$v['type'],'create_time'=>['between',[strtotime(date('Y-m-d')),strtotime(date('Y-m-d')) + 24*3600]]]);
                $v['hasnum'] = $hasnum;
            }
            $data['work_hard'] = $work_hard;

            $temple_interaction = $cmodel->findData('temple_interaction','cname','cvalue');
            if(empty($temple_interaction['cvalue'])){
                $temple_interaction = config('quality.temple_interaction');
            }else{
                $temple_interaction = unserialize($temple_interaction['cvalue']);
            }
            foreach($temple_interaction as &$v){
                $hasnum = $model->getAllCount(['mid'=>$user['id'],'type'=>$v['type'],'create_time'=>['between',[strtotime(date('Y-m-d')),strtotime(date('Y-m-d')) + 24*3600]]]);
                $v['hasnum'] = $hasnum;
            }
            $data['temple_interaction'] = $temple_interaction;

            $glance_content = $cmodel->findData('glance_content','cname','cvalue');
            if(empty($glance_content['cvalue'])){
                $glance_content = config('quality.glance_content');
            }else{
                $glance_content = unserialize($glance_content['cvalue']);
            }
            foreach($glance_content as &$v){
                $hasnum = $model->getAllCount(['mid'=>$user['id'],'type'=>$v['type'],'create_time'=>['between',[strtotime(date('Y-m-d')),strtotime(date('Y-m-d')) + 24*3600]]]);
                $v['hasnum'] = $hasnum;
            }
            $data['glance_content'] = $glance_content;

            $account_information = $cmodel->findData('account_information','cname','cvalue');
            if(empty($account_information['cvalue'])){
                $account_information = config('quality.account_information');
            }else{
                $account_information = unserialize($account_information['cvalue']);
            }
            foreach($account_information as &$v){
                if($v['type'] == 36){
                    $hasnum = $model->getAllCount(['mid'=>$user['id'],'type'=>$v['type'],'create_time'=>['between',[strtotime(date('Y-m-d')),strtotime(date('Y-m-d')) + 24*3600]]]);
                }else{
                    $hasnum = $model->getAllCount(['mid'=>$user['id'],'type'=>$v['type']]);
                    if($v['type'] == 34){
                        $wechat = $wmodel->findData($user['id'],'memid');
                        if(!empty($wechat)){
                            $hasnum = 1;
                        }
                    }elseif($v['type'] == 35){
                        $qq = $qmodel->findData($user['id'],'mid');
                        if(!empty($qq)){
                            $hasnum = 1;
                        }
                    }
                }
                $v['hasnum'] = $hasnum;
            }
            $data['account_information'] = $account_information;
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败'.$e->getMessage());
        }
        return admin_json(1, '成功',$data);
    }

    /**
     * [add  添加]
     * @param 4分享解惑 18分享每日一禅 20分享寺院 26转发分享 32听禅乐
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $user = $this->user;
        try{
            $type = $param['type'];
            if($type == 4){
                $ftype = 1;
            }elseif($type == 18){
                $ftype = 2;
            }elseif($type == 20){
                $ftype = 3;
            }elseif($type == 26 || $type = 32){
                $ftype = 4;
            }
            add_quality($user['id'],$type,$user['quality'],$ftype);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败'.$e->getMessage());
        }
        return admin_json(1, '成功');
    }
    /**
     * [signin  签到]
     * @param
     * @return json
     * @author [C。]
     */
    public function signin()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new MemberQualityModel;
        $mmodel = new MemberModel;
        $cmodel = new ConfigModel;
        try{
            $daily_sign = $cmodel->findData('daily_sign','cname','cvalue');
            if(empty($daily_sign['cvalue'])){
                $daily_sign = config('quality.daily_signin');
            }else{
                $daily_sign = unserialize($daily_sign['cvalue']);
            }

            foreach($daily_sign as &$v){
                if($param['day'] == $v['day']){
                    if($v['is_double'] == 1){
                        $param['quality'] = $v['quality'] * 2;
                    }else{
                        $param['quality'] = $v['quality'];
                    }
                }
            }

            $quality = $model->findOne(['mid'=>$user['id'],'type'=>40,'create_time'=>['between',[strtotime(date('Y-m-d')),strtotime(date('Y-m-d'))+24*3600]]]);
            if(!empty($quality)){
                return admin_json(-2, '今日已签到');
            }

            $param['mid'] = $user['id'];
            $param['type'] = 40;
            $model->addData($param);
            $mmodel->editData(['id'=>$user['id'],'quality'=>$user['quality'] + $param['quality']]);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败'.$e->getMessage());
        }
        return admin_json(1, '成功');
    }
}